package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@RabbitListener(queues = "order.release.order.queue")
@Service
public class OrderCloseListener {
    @Autowired
   OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity entity, Channel channel, Message message)throws IOException {
        System.out.println("收到过期的订单信息：准备关闭订单："+entity);
        try {
            //关闭订单
            orderService.closeOrder(entity);
            //TODO 手动调用支付宝提供的收单接口  C310-8:24
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            //如果关单业务出现异常 则让消息重新回到消息队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

}
